package cn.keepme.service;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.keepme.dao.LogDAO;
import cn.keepme.dao.VisitLogDAO;
import cn.keepme.entity.HackerInterfaceLog;
import cn.keepme.entity.HackerInterfaceLogError;
import cn.keepme.entity.HackerReportLog;
import cn.keepme.entity.HackerReportLogSP;
import cn.keepme.entity.HackerVisitLog;

@Component
@Transactional
public class InterfaceService {
	
	@Resource
	private LogDAO logDao;
	@Resource
	private VisitLogDAO visitLogDao;
	
	/**
	 * 记录访问日志
	 * @param v
	 * @throws Exception
	 */
	public void WriteVisitLog(HackerVisitLog oplog) throws Exception{
		visitLogDao.save(oplog);
	}
	/*
	public void WriteVisitLog(HackerVisitLogVo v) throws Exception{
		HackerVisitLog oplog = new HackerVisitLog();
		oplog.setType(v.getType());
		oplog.setSerial_sys(v.getSerial_sys());
		oplog.setSerial(v.getSerial());
		oplog.setApp_id(v.getApp_id());
		oplog.setCode_id(v.getCode_id());
		oplog.setImsi(v.getImsi());
		oplog.setImei(v.getImei());
		oplog.setResultcode(v.getResultcode());
		oplog.setCreate_time(v.getCreate_time()); // 当前时间
		visitLogDao.save(oplog);
	}
	*/
	/**
	 * 记录接口调用日志
	 * @param v
	 * @throws Exception
	 */
	public void WriteInterfaceLog(HackerInterfaceLog v) throws Exception{
//		HackerInterfaceLog oplog = new HackerInterfaceLog();
//		oplog.setType(v.getType());
//		oplog.setSerial(v.getSerial());
		v.setCreate_time(new Timestamp(new Date().getTime()));
		logDao.save(v);
	}
	/**
	 * 记录接口调用日志
	 * @param v
	 * @throws Exception
	 */
	public void WriteInterfaceLogError(HackerInterfaceLogError v) throws Exception{
		v.setCreate_time(new Timestamp(new Date().getTime()));
		logDao.save(v);
	}
	
	/**
	 * 查访问日志
	 * @param serial
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws InstantiationException
	 */
	public HackerVisitLog queryVisitLog(String serial) 
			throws IllegalAccessException, InvocationTargetException,
			InstantiationException{
		List params=new ArrayList();
		String hql="from HackerVisitLog g where g.serial=?";
//		String sql="select type,app_id,code_id from hacker_log_visit where serial=?";
		params.add(serial);
//		HackerVisitLog v=(HackerVisitLog) visitLogDao.queryObjectBySql(sql, params);
//		HackerVisitLog v = (HackerVisitLog) visitLogDao.queryObjectByHql(hql, params);
//		System.out.println(obj[0]);
		
//		String hql="from Goods g where g.goodsId=? or goodsId=?";
		List<HackerVisitLog> list=visitLogDao.queryListByHql(hql,params);
		HackerVisitLog v = null;
		if(list.size()!=0){
			v = list.get(0);
		}
		return v;
	}
	
	/**
	 * 记录回调Report日志
	 * @param v
	 * @throws Exception
	 */
	public void WriteReportLog(HackerReportLog v) throws Exception{
		logDao.save(v);
	}
	/**
	 * 记录回传PayMax日志
	 * @param v
	 * @throws Exception
	 */
	public void WriteReportLogSp(HackerReportLogSP v) throws Exception{
		logDao.save(v);
	}
	
}
